/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.manto.CarreraForm;
import com.uca.sisbec.helpers.manto.CarreraHelper;
import com.uca.sisbec.mapping.Becario;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author Susy
 */
public class CarreraAction extends HibernateAction {

    public ActionForward init(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        CarreraForm cf = (CarreraForm) form;
        cf.setEstado(true);
        CarreraHelper helper = new CarreraHelper(getSession(request));
        request.setAttribute("CarrerasList", helper.getAllCarreras(cf));
        request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
        return mapping.findForward("success");
    }

    public ActionForward EliminarCarrera(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        CarreraForm cf = (CarreraForm) form;
        CarreraHelper helper = new CarreraHelper(getSession(request));
        ActionMessages msg = new ActionMessages();
        helper.EliminarCarreraDB(Integer.parseInt(request.getParameter("cod")));
        getSession(request).flush();
        request.setAttribute("CarrerasList", helper.getAllCarreras(cf));
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("carrera.eliminada"));
        request.setAttribute("msg", msg);
        return mapping.findForward("success");
    }
    
     public ActionForward GuardarCarrera(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        CarreraForm cf = (CarreraForm) form;
        CarreraHelper helper = new CarreraHelper(getSession(request));
        ActionMessages msg = new ActionMessages();
        helper.InsertarCarreraDB(cf);
        getSession(request).flush();
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("carrera.nueva"));
        request.setAttribute("msg", msg);
        request.setAttribute("CarrerasList", helper.getAllCarreras(cf));
        request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
        return mapping.findForward("success");
    }
     
     public ActionForward CambiarCarrera(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
         if (!isBecarioInSession(request)) return mapping.findForward("searchBecario");
         CarreraForm cf = (CarreraForm) form;
         Becario bec = (Becario) request.getSession().getAttribute(_BECARIO);
         CarreraHelper helper = new CarreraHelper(getSession(request));
         helper.obtenerCarreraBecario(cf,bec);
         request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
         request.setAttribute("CarrerasList",helper.getAllCarreras(cf));
         request.setAttribute("cxbList",helper.obtenerCarrerasBec(bec));
         return mapping.findForward("change");
     }
      public ActionForward addCarrera(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
         CarreraForm cf = (CarreraForm) form;
         Becario bec = (Becario) request.getSession().getAttribute(_BECARIO);
         CarreraHelper helper = new CarreraHelper(getSession(request));
         request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
         request.setAttribute("CarrerasList",helper.getAllCarreras(cf));
         request.setAttribute("cxbList",helper.obtenerCarrerasBec(bec));
         return mapping.findForward("change");
     }
     
     public ActionForward cambiar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
         CarreraForm cf = (CarreraForm) form;
         Becario bec = (Becario) request.getSession().getAttribute(_BECARIO);
         ActionMessages msg = new ActionMessages();
         CarreraHelper helper = new CarreraHelper(getSession(request));
         if(!helper.validarCarrera(cf,bec)){
            request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
            request.setAttribute("CarrerasList",helper.getAllCarreras(cf));
            request.setAttribute("cxbList",helper.obtenerCarrerasBec(bec));
            helper.obtenerCarreraBecario(cf,bec);
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("carrera.existe"));
         }else{
            helper.cambiarBecarioCarrera(cf,bec);
            getSession(request).flush();
            request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
            request.setAttribute("CarrerasList",helper.getAllCarreras(cf));
            request.setAttribute("cxbList",helper.obtenerCarrerasBec(bec));
            helper.obtenerCarreraBecario(cf,bec);
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("carrera.nueva"));
         }
         return mapping.findForward("change");
     }
     
      public ActionForward InhabilitarCarrera(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
          System.out.println("Entre a Inhabilitar carrera");
        CarreraForm cf = (CarreraForm) form;
        ActionMessages msg = new ActionMessages();
        CarreraHelper helper = new CarreraHelper(getSession(request));
        helper.InhabilitarCarreraDB(Integer.parseInt(request.getParameter("cod")),Integer.parseInt(request.getParameter("idUniv")));
        getSession(request).flush();
        request.setAttribute("CarrerasList", helper.getAllCarreras(cf));
        request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("universidad.desactivada"));
        request.setAttribute("msg", msg);
        return mapping.findForward("success");
    }
    
    public ActionForward HabilitarCarrera(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionMessages msg = new ActionMessages();
        CarreraForm cf = (CarreraForm) form;
        CarreraHelper helper = new CarreraHelper(getSession(request));
        helper.HabilitarCarreraDB(Integer.parseInt(request.getParameter("cod")),Integer.parseInt(request.getParameter("idUniv")));
        getSession(request).flush();
        request.setAttribute("CarrerasList", helper.getAllCarreras(cf));
        request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("universidad.activada"));
        request.setAttribute("msg", msg);
        return mapping.findForward("success");
    }
}
